package au.com.evoCoach.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import au.com.evoCoach.domain.Coach;
import au.com.evoCoach.form.LoginForm;
import au.com.evoCoach.service.CoachService;

@Controller
public class Login {
	private CoachService coachService;
	
	private Login() {}
	
	@Autowired
	public Login(CoachService coachService) {
		this.coachService = coachService;
	}
	
	@RequestMapping("/submitLogin.action")
	public String submitLogin(LoginForm loginForm, HttpSession session, Map model) {
		
		Coach coach = coachService.authenticate(loginForm.getCoachName(), loginForm.getPassword());
		
		if (coach == null) {
			model.put("loginError", Boolean.TRUE);
			
			return "login";
		}
		
		session.setAttribute("loggedInCoach", coach);
		model.put("loginSuccess", Boolean.TRUE);
		
		return "redirect:displayHomePage.action";
	}
	
	@RequestMapping("/displayLogin.action")
	public String displayLogin(Map model) {
		
		model.put("loginForm", new LoginForm());
		
		return "login";
	}	
	
}
